package com.aisilesn.base.service;

import com.aisilesn.base.dao.LabelDao;
import com.aisilesn.base.pojo.Label;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: Mr.liu
 * @Description:
 * @Date: Create in 21:47 2019/4/18
 */
@Service
public class LabelService {
    @Autowired
    LabelDao labelDao;

    @Autowired
    IdWorker idWorker;

    /**
     * 查询全部
     * @return
     */
    public List<Label> findAll(){
        return labelDao.findAll();
    }

    /**
     * 根据ID查询对象
     * @param id
     * @return
     */
    public Label findById(String id){
        return labelDao.findById(id).get();
    }

    /**
     * 添加
     * @param label
     */
    public void add(Label label){
        label.setId(idWorker.nextId()+"");
        labelDao.save(label);
    }

    /**
     * 修改
     * @param label
     */
    public void update(Label label){
        labelDao.save(label);
    }

    /**
     * 删除
     * @param id
     */
    public void deleteById(String id){
        labelDao.deleteById(id);
    }

    /**
     * 条件查询
     * @param searchMap
     * @return
     */
    public List<Label> findSearch(Map searchMap){
        return labelDao.findAll(getSpecification(searchMap));
    }
    public Page<Label> findSearch(Map searchMap, int page, int size){
        PageRequest pageRequest = PageRequest.of(page,size);
        return labelDao.findAll(getSpecification(searchMap),pageRequest);
    }
    //拼装查询条件
    public Specification<Label> getSpecification(Map searchMap){
       return new Specification<Label>() {
            @Override
            public Predicate toPredicate(Root<Label> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //拼条件
                List<Predicate> list = new ArrayList<>();
                String labelname = (String) searchMap.get("labelname");//标签名
                String state = (String) searchMap.get("state");//状态,0无效,1,有效
                String recommend = (String) searchMap.get("recommend");//是否推荐,0不推荐,1推荐
                if(!StringUtils.isEmpty(labelname)){
                    list.add(cb.like(root.get("labelname"),"%"+labelname+"%"));//添加条件
                }
                if(!StringUtils.isEmpty(state)){
                    list.add(cb.equal(root.get("state"),state));//添加条件
                }
                if(!StringUtils.isEmpty(recommend)){
                    list.add(cb.equal(root.get("recommend"),recommend));//添加条件
                }
                Predicate and = cb.and(list.toArray(new Predicate[list.size()]));
                return and;
            }
        };
    }
}
